<?php

namespace app\api\validate;
//TODO:删除use示例
use app\common\model\Config as ConfigModel;

class Sample extends ApiBase
{
    protected $regex = ['sample' => '/^sampl?e$/','test' => '/^tes?t$/'];
    // 验证规则
    protected $rule = [
        'sample'       => 'require|checkExist|regex:sample',
        'test'       => 'require|regex:test',
    ];

    // 验证提示
    protected $message = [
        'sample.require'       => '缺少sample.require参数',
        'sample.regex'       => '缺少sample.regex',
        'test.require'       => '缺少test.require参数',
        'test.regex'       => '缺少test.regex参数',
    ];

    // 应用场景
    protected $scene = [
        'sample'          => ['test','sample'],
    ];

    //查询数据重复
    public function checkExist($value, $rule, $data = [], $field = ''){

        if ($field == 'name') {
            $where[] = ['name','=',$value];
            /*$where[] = ['module','=',$data['module']];
            $where[] = ['type','=',$data['type']];*/
            isset($data['id']) &&  $where[] = ['id','<>',$data['id']];
            $total =  ConfigModel::where($where)->count();
            if ($total > 0) {
                return '数据已存在';
            }
        }
        return true;
    }
}